package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.StatFs;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ConvertUtils;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.ApkManageActivity;
import flc.ast.activity.AudioManageActivity;
import flc.ast.activity.DocManageActivity;
import flc.ast.activity.PhoneMemoryActivity;
import flc.ast.activity.PicManageActivity;
import flc.ast.activity.VideoManageActivity;
import flc.ast.databinding.FragmentFileBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.Str2NumUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FileFragment extends BaseNoModelFragment<FragmentFileBinding> {

    @Override
    protected int onCreate() {
        return R.layout.fragment_file;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llPhoneMemory.setOnClickListener(this);
        mDataBinding.llSdMemory.setOnClickListener(this);
        mDataBinding.llPicture.setOnClickListener(this);
        mDataBinding.llVideo.setOnClickListener(this);
        mDataBinding.llAudio.setOnClickListener(this);
        mDataBinding.llDocument.setOnClickListener(this);
        mDataBinding.llApp.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llPhoneMemory://手机内存
                getPermission();
                break;
            case R.id.llPicture://图片
                startActivity(PicManageActivity.class);
                break;
            case R.id.llVideo://视频
                startActivity(VideoManageActivity.class);
                break;
            case R.id.llAudio://音频
                startActivity(AudioManageActivity.class);
                break;
            case R.id.llDocument://文档
                startActivity(DocManageActivity.class);
                break;
            case R.id.llApp://文档
                startActivity(ApkManageActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        //已用百分比
        int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());
        mDataBinding.tvPhoneMemorise.setText(getString(R.string.memory_text1) + ConvertUtils.byte2FitMemorySize(usedLong, 0) + "/" + ConvertUtils.byte2FitMemorySize(allLong, 0));
        mDataBinding.sbPhoneProgress.setProgress(progress);
        mDataBinding.tvPhoneProgress.setText(progress + getString(R.string.unit_percent));
        getSdData();
    }

    private void getSdData() {
        List<StatFs> sdStatFs = StorageUtil.getSDCardMemory(mContext);
        if (CollectionUtils.isNotEmpty(sdStatFs)) {
            StatFs statFs = sdStatFs.get(0);
            //已使用
            long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
            //总内存
            long allLong = statFs.getTotalBytes();

            int progress = Math.round((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());

            mDataBinding.tvSdMemorise.setText(getString(R.string.memory_text1) + ConvertUtils.byte2FitMemorySize(usedLong, 0) + "/" + ConvertUtils.byte2FitMemorySize(allLong, 0));
            mDataBinding.sbSdProgress.setProgress(progress);
            mDataBinding.tvSdProgress.setText(progress + getString(R.string.unit_percent));
        } else {
            mDataBinding.tvSdProgress.setText(getString(R.string.sd_card_not_detected));
            mDataBinding.tvSdMemorise.setVisibility(View.GONE);
            mDataBinding.sbSdProgress.setVisibility(View.GONE);
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc(getString(R.string.get_manage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(PhoneMemoryActivity.class);
                    }
                })
                .request();
    }

}